package com.arpa.oms.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.util.CommonUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.oms.domain.dto.OmsInboundItemDTO;
import com.arpa.oms.domain.entity.OmsInboundItem;
import com.arpa.oms.domain.vo.OmsInboundItemVO;
import com.arpa.oms.mapper.OmsInboundItemMapper;
import com.arpa.oms.service.IOmsInboundItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * OMS入库单明细 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2020-10-22
 */
@Service("OmsInboundItemService")
@Log4j2(topic = "business")
public class OmsInboundItemServiceImpl extends ServiceImpl<OmsInboundItemMapper, OmsInboundItem> implements IOmsInboundItemService {


    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(OmsInboundItem entity) {
        if(StringUtils.isBlank(entity.getCode())){
            entity.setCode(IdUtil.simpleUUID());
        }
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
        }
        return super.save(entity);
    }

    /**
     * 根据code更新实体
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public int updateByCode(OmsInboundItem entity) {
        if(StringUtils.isBlank(entity.getModifiedBy())){
            entity.setModifiedBy(UserUtil.getCode());
        }
        return baseMapper.update(entity,new QueryWrapper<OmsInboundItem>().lambda().eq(OmsInboundItem::getCode,entity.getCode()));
    }

    /**
     * 查询列表
     * @return
     */
    @Override
    public List<OmsInboundItemVO> queryList(OmsInboundItemDTO omsInboundItemDTO){
        //排序字段名需要驼峰转数据库下划线类型字段名
        if(StringUtils.isNotEmpty(omsInboundItemDTO.getSortField())){
            omsInboundItemDTO.setSortField(CommonUtil.camel2Underline(omsInboundItemDTO.getSortField()));
        }
        List<OmsInboundItemVO> omsInboundItemVOList = baseMapper.queryList(omsInboundItemDTO);
        return omsInboundItemVOList;
    }

    /**
     * 查询合计,包含总数
     * @return
     */
    @Override
    public OmsInboundItemVO queryListSum(OmsInboundItemDTO omsInboundItemDTO){
        OmsInboundItemVO omsInboundItemVO = baseMapper.queryListSum(omsInboundItemDTO);
        return omsInboundItemVO;
    }
}
